<template>
  <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="demo-ruleForm" label-width="75px">
    <el-form-item label="原密码" prop="pass">
      <el-input v-model="oldPass" autocomplete="off"
                show-password type="password"></el-input>
    </el-form-item>
    <el-form-item label="新密码" prop="checkPass">
      <el-input v-model="ruleForm.newPass" autocomplete="off"
                show-password type="password"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
      <el-input v-model="ruleForm.checkPass" autocomplete="off" type="password"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button style="margin-left: 10%" type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button style="margin-left: 20%" @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import encrypt from "@/mixin/encrypt";
export default {
  name: "changePassword",
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('密码不能为空'));
      } else {
        const reg = /^\w{8,16}$/;
        if (!reg.test(value)) {
          callback(new Error('请输入仅包含数字和英文字母在内的8-16为密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.newPass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      oldPass: '',
      ruleForm: {newPass: '', checkPass: ''},
      rules: {
        newPass: [{ validator: validatePass, trigger: 'blur' }],
        checkPass: [{ validator: validatePass2, trigger: 'blur' }],
      }
    }
  },
  mixins: [encrypt],
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) {
          await this.$alert('请检查输入内容', '输入验证不通过')
          return false
        } else {
          try {
            const encrypted1 = await this.encrypt(this.oldPass);
            const encrypted2 = await this.encrypt(this.ruleForm.newPass);

            try {
              const res = await this.$axios.put(this.$store.state.serverUrl+'/user/changePassword',
                  {oldPass: encrypted1, newPass: encrypted2}, {
                    headers: {Authorization: localStorage.getItem('token')
                    }});
              if (res.data.status === 1) {
                await this.$alert(res.data.msg, '修改失败')
              } else if (res.data.status === 2) {
                const chance = res.data.msg;
                if (chance === 0) {
                  this.$store.state.username = '';
                  localStorage.removeItem('token');
                  this.$store.commit('updateToken');
                  await this.$alert('账号已被锁定并强制下线，如果有疑问请联系管理员', '验证失败');
                  this.$emit('closeChangePassDialog');
                } else {
                  await this.$alert(`密码输入错误，您还有${chance}次机会`, '验证失败')
                }
              } else {
                this.$message.success('密码修改成功！');
                this.$emit('closeChangePassDialog');
              }
            } catch (err) {
              await this.$alert(err.message, '网络连接出错')
            }
          } catch (err) {
            await this.$alert(err.message, '加密失败');
          }
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    }
  }
}
</script>

<style scoped>

</style>